if [ -d $HOME/.bin ]; then
    export PATH=$HOME/.bin:$PATH
fi

fpath=(/etc/zsh/functions $fpath)

autoload -U zutil
autoload -U compinit
autoload -U complist

#-----------------------------
# colors
#-----------------------------
LS_COLORS='rs=0:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:';
export LS_COLORS

#------------------------------
# Keybindings
#------------------------------
bindkey -v
typeset -g -A key
#bindkey '\e[3~' delete-char
bindkey '\e[1~' beginning-of-line
bindkey '\e[4~' end-of-line
#bindkey '\e[2~' overwrite-mode
bindkey '^?' backward-delete-char
bindkey '^[[1~' beginning-of-line
bindkey '^[[5~' up-line-or-history
bindkey '^[[3~' delete-char
bindkey '^[[4~' end-of-line
bindkey '^[[6~' down-line-or-history
bindkey '^[[A' up-line-or-search
bindkey '^[[D' backward-char
bindkey '^[[B' down-line-or-search
bindkey '^[[C' forward-char
# for rxvt
bindkey "\e[8~" end-of-line
bindkey "\e[7~" beginning-of-line
# for gnome-terminal
bindkey "\eOH" beginning-of-line
bindkey "\eOF" end-of-line

#------------------------------
# Window title
#------------------------------
case $TERM in
  termite|*xterm*|rxvt|rxvt-unicode|rxvt-256color|rxvt-unicode-256color|(dt|k|E)term)
    precmd () {
      print -Pn "\e]0;%n: %~ \a"
    }
    preexec () { print -Pn "\e]0;$1\a" }
    ;;
  screen|screen-256color)
    precmd () {
      print -Pn "\e]0;%n: %~ \a"
    }
    preexec () {
      print -Pn "\e]0;$1\a"
    }
    ;;
esac

# Quick find
f() {
    echo "find . -iname \"*$1*\""
    find . -iname "*$1*"
}

export PAGER="less"
export LESS="-iMSx4 -FX"
  if [[ -x $(which lesspipe.sh) ]] then
    LESSOPEN="| lesspipe.sh %s"
    export LESSOPEN
  fi
alias lv="less"

# Set default editor
if [[ -x $(which emacsclient) ]]
then
    export EDITOR="emacsclient"
    export USE_EDITOR=$EDITOR
    export VISUAL="emacsclient"
    export ALTERNATE_EDITOR="emacs -nw"
fi

# Watch other user login/out
watch=notme
export LOGCHECK=60

# Enable color support of ls
if [[ "$TERM" != "dumb" ]]; then
    if [[ -x `which dircolors` ]]; then
        eval `dircolors -b`
        alias 'ls=ls --color=auto'
    fi
fi

#------------------------------
# ShellFuncs
#------------------------------
# -- coloured manuals
man() {
  env \
    LESS_TERMCAP_mb=$(printf "\e[1;31m") \
    LESS_TERMCAP_md=$(printf "\e[1;31m") \
    LESS_TERMCAP_me=$(printf "\e[0m") \
    LESS_TERMCAP_se=$(printf "\e[0m") \
    LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
    LESS_TERMCAP_ue=$(printf "\e[0m") \
    LESS_TERMCAP_us=$(printf "\e[1;32m") \
    man "$@"
}

# For quickly plotting data with gnuplot.  Arguments are files for 'plot "" with lines'.
plot () {
   echo -n '(echo set term png; '
   echo -n 'echo -n plot \"'$1'\" with lines; '
   for i in $*[2,$#@]; echo -n 'echo -n , \"'$i'\" with lines; '
   echo 'echo ) | gnuplot | display png:-'

   (
        echo "set term png"
        echo -n plot \"$1\" with lines
        for i in $*[2,$#@]; echo -n "," \"$i\" "with lines"
        ) | gnuplot | display png:-
}

# Rotate a jpeg, losslessly
jrotate-r(){
   for i in $*; do
        exiftran -9 -b -i $i
   done
}

# Clean all *.pyc, *.pyo and __pycache__
pyclean(){
    find . -type f -name "*.py[co]" -delete
    find . -type d -name "__pycache__" -delete
}

# Activation
compinit

# Resource files
for file in /etc/zsh/rc/*.rc; do
	source $file
done
